package com.xgpro.mobile.studycentre.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xgpro.common.enums.ResultCodeEnum;
import com.xgpro.common.exceptions.BusinessRuntimeException;
import com.xgpro.domain.studycentre.entity.Tea;
import com.xgpro.domain.studycentre.vo.TeaFront;
import com.xgpro.domain.studycentre.vo.TeaInfoBack;
import com.xgpro.domain.studycentre.vo.TeaSimpeBack;
import com.xgpro.domain.studycentre.vo.TeaSimple;
import com.xgpro.mapper.studycentre.CourseMapper;
import com.xgpro.mapper.studycentre.TeaMapper;
import com.xgpro.mobile.studycentre.service.TeaService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author tx
 * @version 1.0
 * @date 2019/10/11 22:09
 */
@Service
@Transactional(readOnly = false)
public class TeaServiceImpl implements TeaService {

    @Resource
    private TeaMapper teaMapper;

    @Resource
    private CourseMapper courseMapper;


    /**
     * 内外部导师列表
     * @param teaFront
     * @return
     */
    @Override
    public TeaSimpeBack qryTeaSimple(TeaFront teaFront) {
        PageHelper.startPage(teaFront.getPage(),teaFront.getPageSize());
        Page<TeaSimple> pages=(Page<TeaSimple>)teaMapper.selectTeaSimple(teaFront);
        Long total = pages.getTotal();
        List<TeaSimple> result = pages.getResult();
        TeaSimpeBack teaSimpeBack = new TeaSimpeBack();
        teaSimpeBack.setTeaSimpleList(result);
        teaSimpeBack.setTotals(total);
        return teaSimpeBack;
    }

    /**
     * 内外部导师详情
     * @param id
     * @return
     */
    @Override
    public TeaInfoBack qryTeaInfoById(Integer id) {
        TeaInfoBack teaInfoBack = teaMapper.selectTeaInfoById(id);
        teaInfoBack.setCourseSimpleList(courseMapper.selectCourseSimple(id));
        teaInfoBack.setGoodCourseList(courseMapper.selectGoodCourse(id));
        return teaInfoBack;
    }

    /**
     * 设置是否在首页讲师风采
     * @param id
     * @return
     */
    @Override
    public Integer setGoodTea(Integer id ,Integer isGood) {
        Integer result=0;
        Example example = new Example(Tea.class);
        example.createCriteria().andEqualTo("userId",id);
        List<Tea> teas = teaMapper.selectByExample(example);
        example=new Example(Tea.class);
        example.createCriteria().andEqualTo("isGood",1);
        List<Tea> teaList =teaMapper.selectByExample(example);
        if(teaList.size()>6){
            throw new BusinessRuntimeException(ResultCodeEnum.FULL_TEA.getCode(),ResultCodeEnum.FULL_TEA.getMessage());
        }
        if(teas.size()>0){
            Tea tea = teas.get(0);
            tea.setIsGood(isGood);
//            if(tea.getIsGood().equals(0)){
//                tea.setIsGood(1);
//            }else{
//                tea.setIsGood(0);
//            }
            result=teaMapper.updateByPrimaryKeySelective(tea);
        }
        return result;
    }

    /**
     * 查询首页导师风采
     * @return
     */
    @Override
    public TeaSimpeBack qryGoodTea() {
        List<TeaSimple> list=teaMapper.selectGoodTea();
        Long total = (long)list.size();
        TeaSimpeBack teaSimpeBack = new TeaSimpeBack();
        teaSimpeBack.setTeaSimpleList(list);
        teaSimpeBack.setTotals(total);
        return teaSimpeBack;
    }


}
